class Admin::ProductsController < Admin::AdministrationController
  
  before_filter :load_product, :only => [ :edit, :update, :destroy ]
  uses_tiny_mce(:options => {:theme => 'advanced',
    :browsers => %w{msie gecko},
    :theme_advanced_toolbar_location => "top",
    :theme_advanced_toolbar_align => "left",
    :theme_advanced_resizing => true,
    :theme_advanced_resize_horizontal => false,
    :paste_auto_cleanup_on_paste => true,
    :theme_advanced_buttons1 => %w{bold italic underline separator justifyleft justifycenter justifyright indent outdent separator bullist numlist separator link unlink image undo redo},
    :theme_advanced_buttons2 => [],
    :theme_advanced_buttons3 => [],
    :plugins => %w{contextmenu paste}},
    :only => [:new, :edit, :create, :update])
  
  def new
    @product = Product.new
  end
  
  def create
    @product = Product.new( params[:product] )
    if @product.save
      flash[:notice] = 'O produto foi salvo com sucesso'
      redirect_to :action => 'index'
    else
      flash[:error] = 'Existem erros no preenchimento do formulário'
      render :action => 'new'
    end
  end
  
  def edit;  end
  
  def update
    if @product.update_attributes( params[:product] )
      flash[:notice] = 'O produto foi atualizado com sucesso'
      redirect_to :action => 'index'
    else
      flash[:error] = 'Existem erros no preenchimento do formulário'
      render :action => 'edit'      
    end
  end
  
  def index
    @products = Product.find(:all)
  end
  
  def destroy
    @product.destroy
    flash[:notice] = 'O produto foi removido do banco de dados'
    redirect_to :action => 'index'
  end
  
  private
  
  def load_product
    @product = Product.find(params[:id])
  end
  
end
